﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

using KaixinRobot;

namespace KaixinHouse
{
    //用户配置信息类
    public class UserConfig
    {
        public static UserConfig _default = new UserConfig();

        protected string fileName = null;

        public UserIdentity master = new UserIdentity();

        public Collection<int> stayList = new Collection<int>(); // 入住用户ID，依次住入用户家中
        public Collection<int> keepList = new Collection<int>(); // 当停在此类用户家中时，该用户不移动

        public int kickGuest = 0;
        public int checkExpired = 0;
        public int moveWait = 3;
        public int rotateSlaveMove = 0;

        public int doMove = 0;
        public int doWork = 1;
        public int doSlave = 0;
        public int doGarden = 1;


        public Collection<int> stealList = new Collection<int>(); //偷窃列表，用户在此表中进行偷窃
        public Collection<int> tcareList = new Collection<int>();//种植列表，会去种植列表中共享地上种庄稼
        public Collection<int> sendList = new Collection<int>();//增送列表，会去自动给列表上的人赠送果实

        protected Collection<Garden.Seed> seedList = new Collection<Garden.Seed>(); //使用种子的列表

        public int stealOthers = 0;//是否偷窃其他用户，默认为不偷窃
        public int watchCrops = 0;//是否监视庄稼状态
        public int sellCrops = 0;//是否自动出售庄稼
        public int sendCrops = 0;//是否自动赠送庄稼

        public Collection<UserIdentity> slaveList = new Collection<UserIdentity>();

        public UserConfig()
        {
        }

        public UserConfig(string filename)
        {
            this.fileName = filename;

            try
            {
                string[] lines = File.ReadAllLines(filename);
                foreach (string line in lines)
                {
                    if (line.StartsWith("Email=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.master.userMail = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("Password=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.master.userPass = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("StayList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GetIntegerListValue(this.stayList, line);
                        continue;
                    }

                    if (line.StartsWith("KeepList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GetIntegerListValue(this.keepList, line);
                        continue;
                    }

                    if (line.StartsWith("StealList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GetIntegerListValue(this.stealList, line);
                        continue;
                    }

                    if (line.StartsWith("CareList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GetIntegerListValue(this.tcareList, line);
                        continue;
                    }

                    if (line.StartsWith("SendList=", StringComparison.OrdinalIgnoreCase))
                    {
                        GetIntegerListValue(this.sendList, line);
                        continue;
                    }

                    if (line.StartsWith("KickGuest=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.kickGuest = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("RotateSlaveMove=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.rotateSlaveMove = GetIntegerValue(line);
                        continue;
                    }

                    //if (line.StartsWith("CallInterval=", StringComparison.OrdinalIgnoreCase))
                    //{
                    //    this.callInterval = GetIntegerValue(line);
                    //    if (this.callInterval > 0)
                    //    {
                    //        _RunTime.setInterval( this.callInterval );
                    //    }
                    //    continue;
                    //}

                    if (line.StartsWith("CheckExpired=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.checkExpired = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("UseSeed=", StringComparison.OrdinalIgnoreCase))
                    {
                        Match match = Regex.Match(GetStringValue(line), @" *([0-9]+) *, *([0-9]+) *, *([\S]*) *");
                        if (match.Success)
                        {
                            Garden.Seed sd = new Garden.Seed();
                            sd.rank = TextParser.GetInteger(match.Result("$1"));
                            sd.seedid = TextParser.GetInteger(match.Result("$2"));
                            sd.name = match.Result("$3");
                            this.seedList.Add(sd);
                        }
                        continue;
                    }

                    if (line.StartsWith("StealOthers=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.stealOthers = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("MoveWait=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.moveWait = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("WatchCrops=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.watchCrops = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("SellCrops=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.sellCrops = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("SendCrops=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.sendCrops = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoWork=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.doWork = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoMove=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.doMove = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoGarden=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.doGarden = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("DoSlave=", StringComparison.OrdinalIgnoreCase))
                    {
                        this.doSlave = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("Slave=", StringComparison.OrdinalIgnoreCase))
                    {
                        Match match = Regex.Match(GetStringValue(line), @" *([0-9]+) *, *([0-9a-zA-Z_\.@]+) *, *([0-9a-zA-Z]+) *");
                        if (match.Success)
                        {
                            UserIdentity slave = new UserIdentity();
                            slave.userIdx = match.Result("$1");
                            slave.userMail = match.Result("$2");
                            slave.userPass = match.Result("$3");
                            this.slaveList.Add(slave);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
            }

            this.sortSeedListByRank();
        }

        public Garden.Seed getProperSeed(int rank)
        {
            //Set Default Value
            Garden.Seed ret = new Garden.Seed();
            ret.rank = 0;
            ret.seedid = 1;
            ret.name = "胡萝卜";
            if (this.seedList.Count == 0) return ret;

            int maxlvl = this.seedList[this.seedList.Count - 1].rank;
            int minlvl = this.seedList[0].rank;
            int curlvl = rank;

            if (curlvl > maxlvl) curlvl = maxlvl;
            if (curlvl < minlvl) curlvl = minlvl;

            foreach (Garden.Seed sd in this.seedList)
            {
                if (curlvl >= sd.rank)
                {
                    ret = sd;
                }
                else
                {
                    break;
                }
            }
            return ret;
        }

        //Sort Seed List by User Rank
        protected void sortSeedListByRank()
        {
            int i = 0, j = 0;
            for (i = 0; i < this.seedList.Count; i++)
            {
                for (j = i + 1; j < this.seedList.Count; j++)
                {
                    if (this.seedList[i].rank > this.seedList[j].rank)
                    {
                        Garden.Seed sd = this.seedList[i];
                        this.seedList[i] = this.seedList[j];
                        this.seedList[j] = sd;
                    }
                }
            }
        }

        public static void GetIntegerListValue(Collection<int> list, string line)
        {
            string[] idlist = GetStringValue(line).Split(',');
            foreach (string id in idlist)
            {
                int value = TextParser.GetInteger(id);
                if (-1 != value)
                {
                    list.Add(value);
                }
            }
        }

        public static bool GetBoolValue(string line)
        {
            return ("1" == GetStringValue(line));
        }

        public static int GetIntegerValue(string line)
        {
            string value = GetStringValue(line);

            return TextParser.GetInteger(value);
        }

        public static string GetStringValue(string line)
        {
            return line.Substring(line.IndexOf('=') + 1);
        }
    }
}
